﻿using System.Text.RegularExpressions;
using System.Windows.Controls;

namespace HandTest
{
    partial class MainWindow
    {
        readonly Regex numRegex = new Regex(@"^\d+$");
        readonly Regex handRegex = new Regex(@"^手牌\d+$");
        readonly Regex comboRegex = new Regex(@"^组合\d+$");

        void AddPoint(TreeView tree, string text)
        {
            if (new Regex(@"^\d+$").IsMatch(text))
            {
                foreach (TreeViewItem item in tree.Items)
                {
                    if (item.Header as string == text)
                    {
                        currentPoint = item;
                        return;
                    }
                }
                currentPoint = new TreeViewItem() { Header = text, IsExpanded = true };
                tree.Items.Add(currentPoint);
            }
        }

        void AddCombo()
        {
            currentCombo = new TreeViewItem() { Header = "组合" + (currentPoint.Items.Count + 1), IsExpanded = true };
            currentPoint.Items.Add(currentCombo);
        }

        void AddHand()
        {
            currentHand = new TreeViewItem() { Header = "手牌" + (currentCombo.Items.Count + 1), IsExpanded = true };
            currentCombo.Items.Add(currentHand);
        }

        void SetCurrent(TreeViewItem current)
        {
            if (current.Parent is TreeViewItem parent)
            {
                if (handRegex.IsMatch(parent.Header as string) && currentTreeID == 1)
                    current = parent;
                else if (numRegex.IsMatch(parent.Header as string) && currentTreeID != 1)
                    current = parent;
            }
            if (handRegex.IsMatch(current.Header as string))
            {
                currentHand = current;
                current = current.Parent as TreeViewItem;
            }
            if (comboRegex.IsMatch(current.Header as string))
            {
                currentCombo = current;
                if (currentTreeID == 1)
                    for (int i = 0; i < current.Items.Count; i++)
                        (current.Items[i] as TreeViewItem).Header = "手牌" + (i + 1);
                current = current.Parent as TreeViewItem;
            }
            if (numRegex.IsMatch(current.Header as string))
            {
                currentPoint = current;
                if (currentTreeID == 1)
                    for (int i = 0; i < current.Items.Count; i++)
                        (current.Items[i] as TreeViewItem).Header = "组合" + (i + 1);
            }
        }
    }
}
